package Client;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

import java.awt.GridBagLayout;

import javax.swing.JComboBox;

import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.DefaultComboBoxModel;

import clasesBasicas.Accion;
import clasesBasicas.Variable;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

public class frameAccion extends JFrame {

	private JPanel contentPane;


	/**
	 * Create the frame.
	 */
	public frameAccion(String id_placa,String id_variable) {
		//		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 358, 185);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[]{0, 0, 0, 0, 0};
		gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0};
		gbl_panel.columnWeights = new double[]{0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		panel.setLayout(gbl_panel);

		JComboBox comboBox = new JComboBox();

		ArrayList <String> list= new ArrayList <String>();
		String cadena="LISTAACCION "+id_placa+" "+id_variable;
		try {
			list=Client.consulta(cadena);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		int i=0;
		String temp = list.get(i);
		ArrayList <Accion> listaacciones= new ArrayList <Accion>();
		//NECESARIO METER EN ARRAYLIST PARA PODER SABER CUANTOS SON Y PODE RHACE RU ARRAY PARA LA TABLA
		while((temp.startsWith("2")==false)&&(temp.startsWith("ELEM ")))
		{
			procesaVariable rec=new procesaVariable(temp);
			Accion atemp=new Accion();

			int cod_var;
			String accion;
			int cod_accion;
			cod_var= Integer.parseInt(rec.getVar(0).substring(5));
			accion= rec.getVar(1);
			cod_accion=Integer.parseInt(rec.getVar(2));

			atemp.setCod_variable(cod_var);
			atemp.setAccion(accion);
			atemp.setCod_accion(cod_accion);
			listaacciones.add(atemp);
			i++;
			temp=list.get(i);
		}
		String acciones[]= new String [listaacciones.size()];
		for(int t=0;t<listaacciones.size();t++)
		{
			acciones[t]=listaacciones.get(t).getAccion();
		}

		//CREA COMBO BOX
		//		comboBox.setModel(new DefaultComboBoxModel(new String[] {"texto1", "texto2"}));
		comboBox.setModel(new DefaultComboBoxModel(acciones));
		GridBagConstraints gbc_comboBox = new GridBagConstraints();
		gbc_comboBox.insets = new Insets(0, 0, 5, 0);
		gbc_comboBox.gridwidth = 4;
		gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox.gridx = 0;
		gbc_comboBox.gridy = 1;
		panel.add(comboBox, gbc_comboBox);
		
				JButton btnVolver = new JButton("Volver");
				btnVolver.addMouseListener(new MouseAdapter() {
					@Override

					public void mouseReleased(MouseEvent e) {
						dispose();
					}
				});
				
						JButton btnSeleccionarAccin = new JButton("Seleccionar Acci\u00F3n");
						btnSeleccionarAccin.addMouseListener(new MouseAdapter() {
							@Override
							public void mouseReleased(MouseEvent arg0) {
								int sel=comboBox.getSelectedIndex();
								String cadena2="ACCION "+id_placa+" "+id_variable+" "+acciones[comboBox.getSelectedIndex()];
								try {
									String respuesta;
									respuesta=Client.consulta(cadena2).get(0);
									if (respuesta.startsWith("205"))
									{
										String seleccion = JOptionPane.showInputDialog(	btnSeleccionarAccin,  "Introduzca el temporizador",JOptionPane.QUESTION_MESSAGE);  // el icono sera un iterrogante
										if(seleccion!=null)
										{
											String cadena3="CONFIRMAR_ACCION "+seleccion;
											String respuesta2;
											respuesta2=Client.consulta(cadena3).get(0);
											if(respuesta2.startsWith("206"))
											{
												//todo correcto , cerrar y confirmar
												JOptionPane.showMessageDialog(null,"OK Accion sobre el sensor confirmada.");
												clienteGUI.window.table.setValueAt(acciones[comboBox.getSelectedIndex()], clienteGUI.window.ysel, 5);
												dispose();
											}
											else if(respuesta2.startsWith("409"))
											{
												JOptionPane.showMessageDialog(null,"ERROR Faltan datos.");
												dispose();
											}

										}
										else if(seleccion==null)
										{

										}

									}
								} catch (Exception e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}

							}
						});
						GridBagConstraints gbc_btnSeleccionarAccin = new GridBagConstraints();
						gbc_btnSeleccionarAccin.insets = new Insets(0, 0, 5, 5);
						gbc_btnSeleccionarAccin.gridx = 1;
						gbc_btnSeleccionarAccin.gridy = 3;
						panel.add(btnSeleccionarAccin, gbc_btnSeleccionarAccin);
				GridBagConstraints gbc_btnVolver = new GridBagConstraints();
				gbc_btnVolver.insets = new Insets(0, 0, 5, 0);
				gbc_btnVolver.gridx = 3;
				gbc_btnVolver.gridy = 3;
				panel.add(btnVolver, gbc_btnVolver);

		JPanel panel_1 = new JPanel();
		contentPane.add(panel_1, BorderLayout.NORTH);

		JLabel lblSeleccionarAccin = new JLabel("Seleccionar acci\u00F3n");
		panel_1.add(lblSeleccionarAccin);
	}

}
